//
//  STMOrderManagementTableViewCell.m
//  StoreManagement
//
//  Created by centling on 16/5/19.
//  Copyright © 2016年 alfred. All rights reserved.
//

#import "STMOrderManagementTableViewCell.h"
#import "STMOrderModifyViewController.h"
#import "STMLookLogisticsViewController.h"

@implementation STMOrderManagementTableViewCell

- (void)awakeFromNib {
    self.flexButton.layer.borderColor = [UIColor redColor].CGColor;
    self.flexButton.layer.borderWidth = 1;
    self.flexButton.layer.cornerRadius = 3;
    self.orderModifyButton.layer.cornerRadius = 3;
    self.orderModifyButton.hidden = YES;
    [self.orderModifyButton addTarget:self action:@selector(orderModifyButtonClick) forControlEvents:UIControlEventTouchUpInside];
}


- (void)setCellData:(STMOrderManagementListdata *)cellData {
    self.shippingCode = cellData.shipping_code;
    self.orderIdLabel.text = cellData.order_id;
    self.dateLabel.text = cellData.add_time;
    STMOrderManagementListGoods *goodsInfo = cellData.goods[0];
    self.goodsNameLabel.text = goodsInfo.goods_name;
    self.goodsPriceLabel.text = goodsInfo.goods_price;
    self.goodsNumLabel.text = goodsInfo.goods_num;
    self.totalPriceLabel.text = [NSString stringWithFormat:@"总计%@件商品%@(含运费：%@)",goodsInfo.goods_num,cellData.goods_total_price,cellData.shipping_fee];
    [self.goodsImageView sd_setImageWithURL:[NSURL URLWithString:goodsInfo.goods_image] placeholderImage:[UIImage imageNamed:@"nopic"]];
    if ([self.changeButtonTitleIndex isEqualToString:@"0"]) {
        self.orderModifyButton.hidden = NO;
        [self.flexButton setTitle:@"取消订单" forState:UIControlStateNormal];
    }
    if ([self.changeButtonTitleIndex isEqualToString:@"1"]) {
        [self.flexButton setTitle:@"发货" forState:UIControlStateNormal];
    }
    if ([self.changeButtonTitleIndex isEqualToString:@"2"]) {
        [self.flexButton setTitle:@"查看物流" forState:UIControlStateNormal];
    }
    if ([self.changeButtonTitleIndex isEqualToString:@"3"]) {
        [self.flexButton setTitle:@"已取消" forState:UIControlStateNormal];
    }
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (void)orderModifyButtonClick {
    STMOrderModifyViewController *orderModifyVC = [[STMOrderModifyViewController alloc] init];
    orderModifyVC.modifyOrderId = self.orderIdLabel.text;
    orderModifyVC.view.backgroundColor = MAINGRAYCOLOR;
    [orderModifyVC customNavigationTitle:@"修改订单"];
    UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    [nav pushViewController:orderModifyVC animated:YES];
}


- (IBAction)flexButtonClick:(id)sender {
    if ([self.changeButtonTitleIndex isEqualToString:@"0"]) {
        if (_orderManagementTableViewCellBlock) {
            _orderManagementTableViewCellBlock(self.orderIdLabel.text);
        }
    } else if ([self.changeButtonTitleIndex isEqualToString:@"2"]) {
        STMLookLogisticsViewController *logisticsVC = [[STMLookLogisticsViewController alloc] init];
        logisticsVC.postId = self.shippingCode;
        [logisticsVC customNavigationTitle:@"查看物流"];
        UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
        [nav pushViewController:logisticsVC animated:YES];
    } else if ([self.changeButtonTitleIndex isEqualToString:@"1"]) {
        [STMNetworkAPI sendOrderWithKey:[UserDefaultHandler getUserDetaultString:kUserKey]
                                orderId:self.orderIdLabel.text
                                success:^(STMLoginModel *result) {
                                    if ([result.code isEqualToString:@"200"]) {
                                        [GlobalUtil HUDShowMessage:@"发货完成" addedToView:[UIApplication sharedApplication].keyWindow.rootViewController.view];
                                        if (_orderManagementTableViewCellBlock) {
                                            _orderManagementTableViewCellBlock(self.orderIdLabel.text);
                                        }
                                    } else {
                                        [GlobalUtil HUDShowMessage:result.message addedToView:[UIApplication sharedApplication].keyWindow.rootViewController.view];
                                    }
                                    
                                }
                                failure:^(NSError *err) {
                                    [GlobalUtil HUDShowMessage:@"请检查网络连接" addedToView:[UIApplication sharedApplication].keyWindow.rootViewController.view];
                                }];
    }
}


@end
